#include <iostream>
#include <vector>
#include <string>
using namespace std;

// 将一个 0~255 的整数转换为 8 位二进制字符串
string toBinaryString(int num) {
    string binary = "";
    for (int i = 0; i < 8; ++i) {
        binary = char('0' + (num & 1)) + binary;
        num >>= 1;
    }
    return binary;
}

int countOnesInIP(const string& ip) {
    int count = 0;
    string segment;
    for (char ch : ip) {
        if (ch == '.' || ch == '\0') {
            int num = stoi(segment);
            string bin = toBinaryString(num);
            for (char c : bin)
                count += (c == '1');
            segment.clear();
        } else {
            segment += ch;
        }
    }

    // 处理最后一个段
    if (!segment.empty()) {
        int num = stoi(segment);
        string bin = toBinaryString(num);
        for (char c : bin)
            count += (c == '1');
    }

    return count;
}

int main() {
    int T;
    cin >> T;

    while (T--) {
        string ip;
        cin >> ip;
        cout << countOnesInIP(ip) << endl;
    }

    return 0;
}